package com.tsc.order.service.impl;

import com.tsc.order.client.StockClient;
import com.tsc.order.mapper.OrderMapper;
import com.tsc.order.pojo.Order;
import com.tsc.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chentl
 * @Date: 2025/08/29/22:00
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private StockClient stockClient;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    @Transactional
    public Order createOrderOne(Order order) {
        // 插入能否成功？
        orderMapper.insert(order);

        // 扣减库存 能否成功？
        stockClient.reduct(order.getProductId());

//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        // 异常
        int a=1/0;

        return order;
    }

    @Override
    @GlobalTransactional
    public Order createOrderTwo(Order order) {
        // 插入能否成功？
        orderMapper.insert(order);

        // 扣减库存 能否成功？
        stockClient.reduct(order.getProductId());

        // 异常
        int a=1/0;

        return order;
    }
}
